package com.zat.mes.andrmes.dofun.work

import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.media.RingtoneManager
import android.os.Handler
import android.text.TextUtils
import android.util.Log
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.base.BaseActivity
import com.zat.mes.andrmes.data.BindMachineBean
import com.zat.mes.andrmes.data.LoginResultBean
import com.zat.mes.andrmes.data.MathineBean
import com.zat.mes.andrmes.data.PackBean
import com.zat.mes.andrmes.dofun.QrScanActivity
import com.zat.mes.andrmes.dofun.main.adapter.BindMachineAdapter
import com.zat.mes.andrmes.dofun.main.vm.BindMachineModel
import com.zat.mes.andrmes.dofun.work.vm.GetPackModel
import com.zat.mes.andrmes.dofun.work.vm.WorkRecordViewModel
import com.zat.mes.andrmes.global.CompanionCode
import com.zat.mes.andrmes.global.CompanionType
import com.zat.mes.andrmes.utils.*
import kotlinx.android.synthetic.main.activity_bind_machine.*
import kotlinx.android.synthetic.main.activity_machine_warn.*
import kotlinx.android.synthetic.main.activity_update_dm_qty.*
import kotlinx.android.synthetic.main.dialog_upate_qty.view.*
import kotlinx.android.synthetic.main.layout_title.*
import permissions.dispatcher.*
import java.util.*

/**
 * 机器预警
 */
@RuntimePermissions
class MachineWarnActivity : BaseActivity() {


    lateinit var bindModel: BindMachineModel
    var machine: MathineBean? = null
    override fun init() {
        setContentView(R.layout.activity_machine_warn)
        rlReturnTitle.visibility = View.VISIBLE
        rlReturnTitle.setOnClickListener { finish() }

        tVtitle.text = "机器预警"

        bindModel = ViewModelProvider(this).get(BindMachineModel::class.java)
        bindModel.getMathine().observe(this, Observer {
            machine = it
            tvMacNoWarn.text = "机器编码："+it.macNo
            closeLoading()
        })



        btnScanMachine.setOnClickListener {
            showCameraWithPermissionCheck()
        }
        bindModel?.getDelBindData().observe(this, Observer {
            showToast(it)

            closeLoading()
        })
        btnMachine.setOnClickListener {
            if (machine==null){
                showToast("请先扫机器码")
                return@setOnClickListener
            }
            bindModel?.insertMachineWarn(machine?.macNo)
        }



    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            val qrCode = data?.getStringExtra(CompanionType.INTENT_EXTRA_QRCODE)
            if (qrCode != null) {
                if (qrCode?.startsWith(CompanionType.STRING_QR_MAC)) {
                    //查询机器
                    showLoading()
                    bindModel.selectMacByQr(qrCode)
                }
            }

        }
    }


    @NeedsPermission(Manifest.permission.CAMERA)
    fun showCamera() {
        try {
            startActivityForResult(
                Intent(this, QrScanActivity::class.java),
                CompanionCode.REQUESTCODE
            )
        } catch (e: Exception) {
            Log.e("activity", e.toString())
        }
    }

    @OnShowRationale(Manifest.permission.CAMERA)
    fun showRationaleForCamera(request: PermissionRequest) {
        request.proceed();
        Log.e("permission", "OnShowRationale")
    }

    @OnPermissionDenied(Manifest.permission.CAMERA)
    fun onCameraDenied() {
        showCameraWithPermissionCheck()
    }

    @OnNeverAskAgain(Manifest.permission.CAMERA)
    fun onCameraNeverAskAgain() {
//        showToast("请到设置打开相机权限")
        SystemActivityUtil.getAppDetailSettingIntent(this)
    }

    @SuppressLint("NeedOnRequestPermissionsResult")
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        onRequestPermissionsResult(requestCode, grantResults)

    }

}
